/*
 * Albareth - an action roleplaying game.
 * Copyright (C) 2008 Jens Anuth
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 *   02110-1301, USA.
 * 
 * $Id: SpellFireball.java 48 2008-05-17 02:04:48Z jens464 $
*/
package albareth.core.action;

import java.util.Iterator;
import java.util.List;

import frosch.util.FToolkit;

import albareth.core.Game;
import albareth.core.GameEffect;
import albareth.core.GameEffectObserver;
import albareth.core.entity.Mobile;
import albareth.frontend.Resources;
import albareth.frontend.TileCollection;
import albareth.util.Location;

public class SpellFireball extends Spell implements GameEffectObserver
{
    private Mobile _caster = null;
    
    public SpellFireball()
    {
        super("Fireball", TileCollection.ICON_FIREBALL);
        setManaRequirement(5);
        setCastingDuration(0);
        setCooldownDuration(2000);
        setRange(8);
        setFlags(NEEDS_LOS|HARMFUL|TARGET_LOC|TARGET_NOSELF);
    }

    protected boolean onInitiate(Mobile src)
    {
        return true;
    }

    protected boolean onExecute(Mobile src)
    {
        GameEffect effect = GameEffect.flying(
                Resources.EFFECT_FIREBALL,
                src.getLocation(), internalTargetLocation, 10f, false, 0
        );
        effect.setObserver(this);
        src.emitEffect(effect);
        
        src.emitSound(Resources.SOUND_FIREBALL);
        
        _caster = src;
        
        return true;
    }

    /**
     * Fireball explosion.
     */
    public void effectFinished(GameEffect effect)
    {
        int fireballDamage = FToolkit.rand(4, 8) + (getSpellStrength()-1) * 2;
        int damageType = 0; //TODO: damage type fire
        
        Location exploLoc = effect.getCurrentLocation();
        
        GameEffect exploEffect = GameEffect.stationary(
                Resources.EFFECT_EXPLOSION, exploLoc, -1
        );
        _caster.emitEffect(exploEffect);
        _caster.emitSound(Resources.SOUND_EXPLO_LARGE);

        
        //direct hit explode cell:
        exploLoc.inflictDamage(
                Math.round(fireballDamage * 1.5f), damageType, _caster);
        //Mobile directMob = exploLoc.getMobile();
        //if (directMob != null) {
        //    directMob.applyDamage(
        //            Math.round(fireballDamage * 1.5f), damageType, _caster
        //    );
        //}
        
        //explode adjacent cells:
        List cellsToExplode = exploLoc.getAllNeighborLocations();
        for (Iterator it = cellsToExplode.iterator(); it.hasNext(); )
        {
            Location loc = (Location)it.next();
            loc.inflictDamage(fireballDamage, damageType, _caster);
            //Mobile indirectMob = loc.getMobile();
            //if (indirectMob != null) {
            //    indirectMob.applyDamage(fireballDamage, damageType, _caster);
            //}
        }
        
    }

}
